package leetcode;

public class LC28 {
    public static void main(String[] args) {
        LC28 lc28 = new LC28();
        ListNode.print(lc28.addTwoNumbers(ListNode.ln(new int[]{9,9,9,9,9,9,9}),ListNode.ln(new int[]{9,9,9,9})));
    }
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ans = new ListNode(-1);
        ListNode tp = ans;
        int temp = 0;
        while(l1 != null && l2 != null){
            int val = l1.val + l2.val + temp;
            tp.next = new ListNode(val % 10);
            temp = val / 10;
            tp = tp.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if (l2 == null){
            l2 = l1;
        }
        while(l2 != null) {
            int val = l2.val + temp;
            tp.next = new ListNode(val % 10);
            temp = val / 10;
            l2 = l2.next;
            tp = tp.next;
        }
        if (temp != 0) tp.next = new ListNode(temp);
        return ans.next;
    }
}
